package com.app.brilliant.game.service.impl;
import com.app.brilliant.common.service.impl.BaseServiceImpl;
import com.app.brilliant.game.bean.BriUserExpInfo;
import com.app.brilliant.game.bean.WxUser;
import com.app.brilliant.game.mapper.BriUserExpInfoMapper;
import com.app.brilliant.game.response.UserRankInfo;
import com.app.brilliant.game.service.BriUserExpInfoService;
import com.app.brilliant.game.service.WxUserService;
import com.app.brilliant.game.vo.UserRankData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * 用户经验信息服务实现类
 */
@Service
public class BriUserExpInfoServiceImpl extends BaseServiceImpl<BriUserExpInfoMapper, BriUserExpInfo> 
        implements BriUserExpInfoService {
    @Autowired
    private WxUserService wxUserService;
    @Override
    public UserRankData getUserRank(Integer limit, Long userId) {
        // 获取排行榜前N名用户
        List<UserRankInfo> topUsers = baseMapper.getTopRankedUsers(limit);
        // 获取当前用户排名信息
        UserRankInfo currentUserRank = null;
        WxUser wxUser = wxUserService.getById(userId);
        if (userId != null) {
            currentUserRank = baseMapper.getUserRankInfo(userId);
            if(currentUserRank == null){
                currentUserRank = new UserRankInfo();
                currentUserRank.setUserId(userId);
                currentUserRank.setRankIndex(-1);
                currentUserRank.setExp(0);
                currentUserRank.setUserName(wxUser.getNickName());
                currentUserRank.setAvatarUrl(wxUser.getAvatarUrl());
            }
        }
        // 构建返回结果
        UserRankData result = new UserRankData();
        result.setTopUsers(topUsers);
        result.setCurrentUser(currentUserRank);
        return result;
    }
}